package matthias.WagoApp;

import android.app.Activity;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class settings_project extends Activity{
	
	public static final String LOG_TAG="WAGOAPP";
	public String ip= "192.168.1.34"; 
	public static Boolean en_sim=false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		loadSettings();
		try {
			setContentView(R.layout.settings_layout);
			
			//get value passed from main activity when start this
			Bundle extras = getIntent().getExtras();
			if (extras != null) {
				ip = extras.getString("ip");
			    en_sim= extras.getBoolean("en_sim");
			}
			
			// assign every value to its object in this page
			CheckBox ckbEnSim =  (CheckBox) findViewById(R.id.checkBox1);
			ckbEnSim.setText(getString(R.string.app_check_box1));
			if (en_sim==true){
				ckbEnSim.setChecked(true);
			}
			else{
				ckbEnSim.setChecked(false);
			}
			ckbEnSim.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					en_sim= isChecked;	
				}
			});
			
			final EditText edtxtIp = (EditText) findViewById(R.id.edtxtIp);
			edtxtIp.setText(ip);
			
			final Button btnSave = (Button) findViewById(R.id.btnSave);
			btnSave.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					ip=edtxtIp.getText().toString();
					saveSettings();
		        	Toast.makeText(getBaseContext(),getString(R.string.app_settings_saved),Toast.LENGTH_LONG).show();
					settings_project.this.finish();					
				}
			});
			
			final Button btnCancel = (Button) findViewById(R.id.btnCAncel);
			btnCancel.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					settings_project.this.finish();					
				}
			});
			
		} catch (Exception e) {
			Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_LONG).show();
		}
	}
	
	protected void onStop(){
        super.onStop();      
    }
	
	public void saveSettings(){
		Log.v(LOG_TAG, "Save Settings");
		SharedPreferences settings = getSharedPreferences("PREFS_WAGOAPP", 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString("ip", ip);
		editor.putBoolean("en_sim", en_sim);
		Log.v(LOG_TAG, "Load Settings IP= "+ip);
		Log.v(LOG_TAG, "Load Settings en_sim " + en_sim);
		// Commit the edits!
		editor.commit();
	}
	public void loadSettings(){
		Log.v(LOG_TAG, "Load Settings");
		// Load settings from device
		try{
		    SharedPreferences settings = getSharedPreferences("PREFS_WAGOAPP", 0);
		    String S_IP= settings.getString("ip",ip);
		    boolean S_EnSim= settings.getBoolean("en_sim",en_sim);
		    ip=S_IP;
		    en_sim= S_EnSim;		  
		    Log.v(LOG_TAG, "Load Settings IP= "+ip);
		    Log.v(LOG_TAG, "Load Settings en_sim " + en_sim);
		}catch (Exception e) {
		       	Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_LONG).show();
	    }
	}
}